import java.io.*;
import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入你要指定的目录");
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        File file = new File(str);
        if(!file.isDirectory()) {
            System.out.println("你输入的不是目录或者该目录不存在");
            return;
        }

        System.out.println("请输入你要指定的内容：");
        String token = scan.next();
        scanDir(file, token);
    }

    public static void scanDir(File rootFile, String token) throws IOException {
        File[] list = rootFile.listFiles();
        for(File file: list) {
            if(file.isDirectory()) {
                scanDir(file, token);
            } else {
                checkFile(file, token);
            }
        }
    }

    private static void checkFile(File file, String token) throws IOException {
        if(file.getName().contains(token)) {
            System.out.println("文件名包含指定关键字：" + file.getAbsolutePath());
            return;
        }
        Reader reader = new FileReader(file);
        StringBuilder check = new StringBuilder();
        char[] b = new char[1024];
        while(true) {
            int n = reader.read(b);
            if(n == -1) {
                break;
            }
            check.append(b);
        }
        if(check.toString().contains(token)) {
            System.out.println("文件内容包含指定关键字：" + file.getAbsoluteFile());
        }
    }
}
